package com.proxy;

import com.sun.org.apache.regexp.internal.RE;

import java.util.List;

/**
 * User: yuanjianpeng
 * Date: 2019/1/12
 * Project: webServiceServerTest
 * Version: 1.0
 * Description: 远程调用模板类
 */
public abstract class InvokeTemplate<REQ, RESP> {

    protected abstract Result invoke(REQ request);

    public Result<RESP> call(REQ req){
        Result remoteResponse = invoke(req);
        return convert(remoteResponse);
    }

    Result<RESP> convert(Result src){
        Result<RESP> result = new Result<>();
        if(null == result){
            src.setErrorCode("null");
            src.setErrorMsg("null");
            return result;
        }
        if(src.isSuccess()){
            result.setResult(convert(src.getResult(), cunstomConvert()));
        }else{
            src.setErrorCode("busness error");
            src.setErrorMsg("busness error");
        }
        return result;
    }

    protected RESP convert(Object src, Converter converter){
        if(null == converter){
            return (RESP)src;
        }
        if(null == src){
            return null;
        }
        Object obj = null;
        if(src instanceof List){
            obj = converter.convertListEtoT((List) src);
        }else{
            obj = converter.convertEtoT(src);
        }
        return (RESP)obj;
    }

    protected Converter cunstomConvert(){
        return null;
    }
}
